package io.renren.modules.backstage.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import com.tencent.common.pojo.Result;
import io.renren.common.exception.StatusCode;
import io.renren.common.utils.OSSUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.backstage.service.VideoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.backstage.entity.VideoEntity;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-04-25 13:39:07
 */
@RestController
@RequestMapping("backstage/video")
public class VideoController {
    @Autowired
    private VideoService videoService;



    /**
     * 检查视频是否已存在
     * @param fileMd5
     * @param fileName
     * @param fileSize
     * @param mimetype
     * @param fileExt
     * @return
     */
    @RequestMapping("/register")
    //@RequiresPermissions("backstage:video:register")
    public Result register(String fileMd5, String fileName, String fileSize, String mimetype, String fileExt){
        Result result = videoService.register(fileMd5, fileName, fileSize, mimetype, fileExt);
        return result;
    }

    /**
     * 分块检查
     * @param fileMd5
     * @param chunk
     * @param chunkSize
     * @return
     */
    @RequestMapping("checkchunk")
    public Result checkchunk(String fileMd5,Integer chunk,Integer chunkSize){
        Result result = videoService.checkchunk(fileMd5, chunk, chunkSize);
        return result;
    }

    @RequestMapping("uploadChunk")
    public Result uploadChunk(MultipartFile file,String fileMd5,Integer chunk){
        Result result = videoService.uploadChunk(file, fileMd5, chunk);
        return result;
    }

    @RequestMapping("mergechunks")
    public Result mergechunks(String fileMd5, String fileName, Long fileSize,String mimetype,String fileExt){
        Result result = videoService.mergechunks(fileMd5, fileName, fileSize, mimetype, fileExt);
        return result;
    }

    @RequestMapping("uploadVideo")
    public Result uploadVideo(@RequestBody MultipartFile file){
        Result result = videoService.uploadVideo(file);
        return result;
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("backstage:video:list")
    public R list(@RequestParam Map<String, Object> params){
        String cid = (String) params.get("cid");
        if(cid!=null&& !cid.equals("")){
            PageUtils page= videoService.queryPage1(params);
            return R.ok().put("page", page);
        }

        PageUtils page = videoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{vid}")
    @RequiresPermissions("backstage:video:info")
    public R info(@PathVariable("vid") Integer vid){

		VideoEntity video = videoService.updateByVideoId(vid);

        return R.ok().put("video", video);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("backstage:video:save")
    public R save(@RequestBody VideoEntity video){
		videoService.save(video);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("backstage:video:update")
    public R update(@RequestBody VideoEntity video){
		videoService.updateById(video);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("backstage:video:delete")
    public R delete(@RequestBody Integer[] vids){
		videoService.removeByIds(Arrays.asList(vids));

        return R.ok();
    }


    /**
     *上传视频
     */
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy/MM/dd");
    @RequestMapping("/upload")
    public com.tencent.common.pojo.Result upload(@RequestBody MultipartFile file, HttpServletRequest request) {
        Map<String,Object> map=new HashMap<>();
        String originalFilename = file.getOriginalFilename();
        if(!originalFilename.endsWith(".mp4")){
            return new Result(StatusCode.ERROR,"文件类型不对",false);
        }
        String format = simpleDateFormat.format(new Date());
        System.out.println("format是:" + format);
        String newName = UUID.randomUUID().toString() + ".mp4";
        String objectName=format+newName;
        Result result = OSSUtils.createOSSClient(objectName, file);
        System.err.println(result.getData().toString());
        return result;
    }

    @RequestMapping("/setUrl")
    public Result setUrl(@RequestParam Map<String, Object> params){
        videoService.setURL(params);
        return new Result(StatusCode.OK,"视频上传成功",true);
    }

}
